<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateSystemSettingsTable extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     */
    public function change()
    {
        $table = $this->table('system_settings', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci']);
        $table->addColumn('key', 'string', ['limit' => 50, 'null' => false, 'comment' => '设置键名'])
            ->addColumn('value', 'text', ['null' => true, 'comment' => '设置值'])
            ->addColumn('description', 'string', ['limit' => 255, 'null' => true, 'comment' => '设置描述'])
            ->addColumn('create_time', 'timestamp', ['null' => true, 'comment' => '创建时间'])
            ->addColumn('update_time', 'timestamp', ['null' => true, 'comment' => '更新时间'])
            ->addIndex(['key'], ['unique' => true])
            ->create();
            
        // 添加默认设置
        $this->table('system_settings')->insert([
            [
                'key' => 'note_price',
                'value' => '1.00',
                'description' => '纸条单价',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ],
            [
                'key' => 'v1_first_level_rate',
                'value' => '0.54',
                'description' => 'V1代理一级佣金比例',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ],
            [
                'key' => 'v2_first_level_rate',
                'value' => '0.65',
                'description' => 'V2代理一级佣金比例',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ],
            [
                'key' => 'v2_second_level_rate',
                'value' => '0.09',
                'description' => 'V2代理二级佣金比例',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ],
            [
                'key' => 'v2_other_level_rate',
                'value' => '0.09',
                'description' => 'V2代理三级及以上佣金比例',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ],
            [
                'key' => 'v3_basic_rate',
                'value' => '0.10',
                'description' => 'V3代理基础佣金比例',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ],
            [
                'key' => 'v3_v1_first_level_rate',
                'value' => '0.21',
                'description' => 'V3代理一级V1用户佣金比例',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ],
            [
                'key' => 'v3_v1_other_level_rate',
                'value' => '0.19',
                'description' => 'V3代理二级及以上V1用户佣金比例',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ]
        ])->saveData();
    }
} 